package jssia.database.mysql;

import jssia.database.mysql.columns.BigIntColumn;
import jssia.database.mysql.columns.BitColumn;
import jssia.database.mysql.columns.BlobColumn;
import jssia.database.mysql.columns.CharColumn;
import jssia.database.mysql.columns.Column;
import jssia.database.mysql.columns.DateColumn;
import jssia.database.mysql.columns.DateTimeColumn;
import jssia.database.mysql.columns.DecimalColumn;
import jssia.database.mysql.columns.DoubleColumn;
import jssia.database.mysql.columns.EnumColumn;
import jssia.database.mysql.columns.FlagColumn;
import jssia.database.mysql.columns.FloatColumn;
import jssia.database.mysql.columns.IntColumn;
import jssia.database.mysql.columns.LongBlobColumn;
import jssia.database.mysql.columns.LongTextColumn;
import jssia.database.mysql.columns.MediumBlobColumn;
import jssia.database.mysql.columns.MediumIntColumn;
import jssia.database.mysql.columns.MediumTextColumn;
import jssia.database.mysql.columns.SetColumn;
import jssia.database.mysql.columns.SmallIntColumn;
import jssia.database.mysql.columns.TextColumn;
import jssia.database.mysql.columns.TimeColumn;
import jssia.database.mysql.columns.TimestampColumn;
import jssia.database.mysql.columns.TinyBlobColumn;
import jssia.database.mysql.columns.TinyIntColumn;
import jssia.database.mysql.columns.TinyTextColumn;
import jssia.database.mysql.columns.UnsignedBigIntColumn;
import jssia.database.mysql.columns.UnsignedDoubleColumn;
import jssia.database.mysql.columns.UnsignedFloatColumn;
import jssia.database.mysql.columns.UnsignedIntColumn;
import jssia.database.mysql.columns.UnsignedMediumIntColumn;
import jssia.database.mysql.columns.UnsignedSmallIntColumn;
import jssia.database.mysql.columns.UnsignedTinyIntColumn;
import jssia.database.mysql.columns.VarCharColumn;
import jssia.database.mysql.columns.YearColumn;
import jssia.database.verification.TypeMap;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

public final class MysqlTypeMap implements TypeMap {

    private final BiMap<String, Class<? extends Column>> mysqlTypeToColumnClass = HashBiMap.<String, Class<? extends Column>>create();

    public MysqlTypeMap() {
        loadMappings();
    }

    @Override
    public Class<? extends Column> getColumnClass(String mysqlType) {
        return mysqlTypeToColumnClass.get(mysqlType);
    }


    @Override
    public String getMysqlType(Class<? extends Column> columnClass) {
        return mysqlTypeToColumnClass.inverse().get(columnClass);
    }

    private void loadMappings() {
        mysqlTypeToColumnClass.put("bigint unsigned", UnsignedBigIntColumn.class);
        mysqlTypeToColumnClass.put("double unsigned", UnsignedDoubleColumn.class);
        mysqlTypeToColumnClass.put("float unsigned", UnsignedFloatColumn.class);
        mysqlTypeToColumnClass.put("int unsigned", UnsignedIntColumn.class);
        mysqlTypeToColumnClass.put("mediumint unsigned", UnsignedMediumIntColumn.class);
        mysqlTypeToColumnClass.put("smallint unsigned", UnsignedSmallIntColumn.class);
        mysqlTypeToColumnClass.put("tinyint unsigned", UnsignedTinyIntColumn.class);
        mysqlTypeToColumnClass.put("bigint", BigIntColumn.class);
        mysqlTypeToColumnClass.put("bit", BitColumn.class);
        mysqlTypeToColumnClass.put("blob", BlobColumn.class);
        mysqlTypeToColumnClass.put("char", CharColumn.class);
        mysqlTypeToColumnClass.put("date", DateColumn.class);
        mysqlTypeToColumnClass.put("datetime", DateTimeColumn.class);
        mysqlTypeToColumnClass.put("decimal", DecimalColumn.class);
        mysqlTypeToColumnClass.put("double", DoubleColumn.class);
        mysqlTypeToColumnClass.put("enum", EnumColumn.class);
        mysqlTypeToColumnClass.put("char(0)", FlagColumn.class);
        mysqlTypeToColumnClass.put("float", FloatColumn.class);
        mysqlTypeToColumnClass.put("int", IntColumn.class);
        mysqlTypeToColumnClass.put("longblob", LongBlobColumn.class);
        mysqlTypeToColumnClass.put("longtext", LongTextColumn.class);
        mysqlTypeToColumnClass.put("mediumblob", MediumBlobColumn.class);
        mysqlTypeToColumnClass.put("mediumint", MediumIntColumn.class);
        mysqlTypeToColumnClass.put("mediumtext", MediumTextColumn.class);
        mysqlTypeToColumnClass.put("set", SetColumn.class);
        mysqlTypeToColumnClass.put("smallint", SmallIntColumn.class);
        mysqlTypeToColumnClass.put("text", TextColumn.class);
        mysqlTypeToColumnClass.put("time", TimeColumn.class);
        mysqlTypeToColumnClass.put("timestamp", TimestampColumn.class);
        mysqlTypeToColumnClass.put("tinyblob", TinyBlobColumn.class);
        mysqlTypeToColumnClass.put("tinyint", TinyIntColumn.class);
        mysqlTypeToColumnClass.put("tinytext", TinyTextColumn.class);
        mysqlTypeToColumnClass.put("varchar", VarCharColumn.class);
        mysqlTypeToColumnClass.put("year", YearColumn.class);
    }
}
